<?php

namespace App\Http\Controllers\Talent;

use App\Http\Controllers\BaseController;
use App\Models\Position;
use App\Models\PositionApply;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PositionController extends BaseController
{
    public function index(Request $request)
    {

        $params = $this->validate($request,[
            'keyword' => 'string|max:100',
            'post_id' => 'integer',
            'firm_profession_id' => 'integer',
            'province' => 'max:30',
            'city' => 'max:30',
            'area' => 'max:30',
        ]);

        $query = Position::query();

        if (isset($params['keyword'])){
            $query->where('name','like','%'.$params['keyword'].'%');
        }

        if (isset($params['post_id'])){
            $query->where('post_id',$params['post_id']);
        }

        if (isset($params['firm_profession_id'])){
            $query->where('firm_profession_id',$params['firm_profession_id']);
        }

        if (isset($params['province'])){
            $query->where('province',$params['province']);
        }

        if (isset($params['city'])){
            $query->where('city',$params['city']);
        }

        if (isset($params['area'])){
            $query->where('area',$params['area']);
        }



        $position = $query->select(['id','name','annual_salary','view_count'])
                    ->where('status',1)
                    ->orderByDesc('id')
                    ->simplePaginate();

        return $this->success($position);

    }

    public function show(Request $request)
    {
        $param = $this->validate($request,[
            'position_id' => 'required|integer'
        ]);

        $position = Position::query()
            ->where('id',$param['position_id'])
            ->first();

        return $this->success($position);

    }

    public function apply(Request $request)
    {
        $param = $this->validate($request,[
            'position_id' => 'required',
            'curriculum_vitae' => 'required',
            'name' => 'required',
        ]);

        $param['user_id'] = auth('api')->id();

        $apply =  PositionApply::query()->create($param);

        return $apply->getKey() ? $this->success() : $this->error();
    }


}
